﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Website.Logger;
using System.Data.SqlClient;
using DAL;
using BOL;
using System.Web.Script.Services;
using System.Web.Services;

namespace UILGestion.Login
{
    public partial class LoginForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    FormatForm();

                }

            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }


            
        }

        private void FormatForm()
    {
            //var q= from a in DbUtil.Db.Empresas
            //       select a;

            //empresaDropDownList.DataTextField = "descripcion";
            //empresaDropDownList.DataValueField = "idInternal";
            //empresaDropDownList.DataSource = q;
            //empresaDropDownList.DataBind();


            
    }

        private void load()
        {
            LoginBL login = new LoginBL();
            if (login.Autenticar(emailTextBox.Text.Trim(), passwordTextBox.Text.Trim()))
            {
                LoginInfo log = login.getUsuario();
                        // guardo valor de cuenta
                        DAL.SistemaActividad nuevo = new SistemaActividad();
                        nuevo.fecha=DateTime.Now;
                        nuevo.usuario=emailTextBox.Text.Trim();
                        nuevo.host = Request.UserHostName + "/" + Request.UserHostAddress;
                        DbUtil.Db.SistemaActividads.InsertOnSubmit(nuevo);
                        DbUtil.Db.SubmitChanges();




                Session["usuario"] = emailTextBox.Text.Trim();
                //Session["empleado_id"] = login.;
                Session["login"] = true;
                // Session["LoginInfo"] = log;
                // Session["login.Empresa.idEmpresa"] = int.Parse(empresaDropDownList.SelectedValue);
                // Session["idEmpresa"] = int.Parse(empresaDropDownList.SelectedValue);
                Session["idEmpleado"] = log.idEmpleado;
                // Session["login.Empresa.descripcion"] = empresaDropDownList.SelectedItem.Text;
                Session["login.Empleado.descripcion"] = log.nombre;

                Response.Redirect("Default.aspx", false);
            }
        }


        protected void Button1_Click(object sender, EventArgs e)
        {
            
            try
            {
                load();
            }
            catch (Exception ex)
            {
                mensajelbl.Text="Error:" + ex.Message ;
                
            }

        }

        protected void cancelarHLK_Click(object sender, EventArgs e)
        {
            emailTextBox.Text = string.Empty;
            passwordTextBox.Text = string.Empty;
        }

        protected void passwordTextBox_TextChanged(object sender, EventArgs e)
        {
            try
            {
                load();
            }
            catch (Exception ex)
            {
                mensajelbl.Text = "Error:" + ex.Message;

            }
            
        }
    }
}                                                      